<template>
  <div class="app-container">
    <el-card class="query-card">
      <el-form ref="query" :model="query" inline label-position="right" label-width="90px">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="query.categoryName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
<!--      <div>
        <span>分类信息列表</span>
      </div>
      <el-divider />-->
      <div class="table-button">
        <el-button icon="el-icon-plus" type="primary" @click="saveClick">新增</el-button>
        <el-button icon="el-icon-d-caret" plain type="primary" @click="expandClick">{{ expandTableTree ? '收起' : '展开' }}
        </el-button>
        <el-button type="danger" plain class="el-icon-folder-delete" @click="handleBatchDelete">批量删除</el-button>
        <el-tooltip content="刷新" class="table-right-btn">
          <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
        </el-tooltip>
      </div>
      <el-table
        v-if="refreshTable"
        v-loading="tableLoading"
        :data="tableData"
        row-key="id"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        :default-expand-all="expandTableTree"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="45" />
        <el-table-column label="分类" prop="categoryName" show-overflow-tooltip	/>
        <el-table-column label="排序" prop="sort" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="saveClick(scope.row)"
            >新增
            </el-button>
            <el-button
              type="text"
              @click="editClick(scope.row)"
            >修改
            </el-button>
            <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='取消'
              placement="top"
              style="margin-left: 10px"
              title="是否删除此条数据"
              @confirm="handleDelConfirm(scope.row)"
            >
              <el-button slot="reference" type="text">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <div v-if="dialogVisible">
      <submit-module ref="submitModule" :visible="dialogVisible" @close="handleClose" />
    </div>
  </div>
</template>

<script>
import { deleteCategory, selectCategoryTree } from '@/api/erp/category'
import SubmitModule from '@/views/erp/category/modules/submitModule.vue'

export default {
  name: 'Index',
  components: { SubmitModule },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      selected: [],
      query: {
        menuName: '',
        menuType: '',
        state: ''
      },
      dialogVisible: false,
      refreshTable: true,
      expandTableTree: false
    }
  },
  mounted() {
    this.doPage()
  },
  methods: {
    handleSearch() {
      this.doPage()
    },
    // 选中事件
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.doPage()
    },
    doPage() {
      this.tableLoading = true
      selectCategoryTree(this.query).then(resp => {
        if (resp.success) {
          this.tableData = resp.data
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 新增按钮点击
    saveClick(row) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(row, null)
      })
    },
    // 编辑按钮
    editClick(row) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(null, row.categoryId)
      })
    },
    // 批量删除点击
    handleBatchDelete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量删除选中的数据?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.categoryId) }
        deleteCategory(data).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
            this.doPage()
          }
        }).finally(() => {
          this.tableLoading = false
        })
      })
    },
    expandClick() {
      this.refreshTable = false
      this.expandTableTree = !this.expandTableTree
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    // 表单关闭
    handleClose() {
      this.dialogVisible = false
      this.doPage()
    },
    handleDelConfirm(row) {
      this.tableLoading = true
      const data = { ids: [row.id] }
      deleteCategory(data).then(resp => {
        if (resp.success) {
          this.$message.success('删除成功')
          this.doPage()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
